﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOMS.DAL;
using System.Data;
using BOMS.Utility;

namespace BOMS.WebUI.Web.RealStatus
{
    public partial class groupPage : System.Web.UI.Page
    {
        public string LanName
        {
            get { return Request.QueryString["lanName"]; }
        }
        /// <summary>
        /// 传入的参数状态.如果未传或空,则默认"ALL"
        /// </summary>
        public string DisplayStatus
        {
         get
         {
             if(string.IsNullOrEmpty(Request.QueryString["status"]))
             {
                 return "ALL";
             }
             else
             {
                return Request.QueryString["status"].ToUpper();//"ONLINE","OFFLINE","ALL"
             }
         }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                // 正确的属性设置方法
                this.GridView_wince.Attributes.Add("SortExpression", "HostName");
                this.GridView_wince.Attributes.Add("SortDirection", "ASC");

                ViewState["SortOrder"] = "HostName";
                ViewState["OrderDire"] = " ASC"; 

                dbind();
            }
        }
        public void dbind()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.GridView_wince.Attributes["SortExpression"];
            string sortDirection = this.GridView_wince.Attributes["SortDirection"];

            // 调用业务数据获取方法
            DataSet ds = new DataSet();
            ds = RealDate.getWinceInfoOfLan();
            DataTable dt=ds.Tables[0];

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }

            this.GridView_wince.DataSource =dt ;
            this.GridView_wince.DataBind();
        }

        protected void GridView_wince_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "detail")
            {
                 int rowIndex = Convert.ToInt32(e.CommandArgument);
                 if (rowIndex > -1)
                 {
                     string mac = this.GridView_wince.DataKeys[rowIndex].Value.ToString();
                     string hn = this.GridView_wince.Rows[rowIndex].Cells[1].Text;
                     string ip = this.GridView_wince.Rows[rowIndex].Cells[2].Text;
                   
                         ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>window.location.href='groupPageList.aspx?winceMac=" + mac + "&winceIp=" + ip + "&WinceName=" + HttpUtility.UrlEncodeUnicode(hn) + "&lanname=" + HttpUtility.UrlEncodeUnicode(LanName) + "';</script>");
                     }
                 }
            
        }

        protected void GridView_wince_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                ShowSortedIcon(GridView_wince, e.Row);
            }
        
            if (e.Row.RowType == DataControlRowType.DataRow)//判断是不是数据行
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#abcdef'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                Literal ltrIsOnline = e.Row.FindControl("ltrIsOnline") as Literal;
                DataRowView drv = e.Row.DataItem as DataRowView;
                if (ltrIsOnline != null
                    && drv != null)
                {
                    DateTime LastTime = (DateTime)drv["LatestTime"];
                    TimeSpan ts = new TimeSpan(0,3,0);//3分钟；
                    OnlineStatus Online = OnlineStatus.离线;
                    if(DateTime.Now>LastTime.Add(ts))//已超时
                    {
                       Online = OnlineStatus.离线;
                    }
                    else{
                       Online = OnlineStatus.在线;
                    }
                    ltrIsOnline.Text = Common.BuildIsOnlineString(Online);

                    //根据页面参数要求，让本行隐藏或显示。
                    e.Row.Visible = IsRowShow(Online);//Added by liangning 20121120 .
                }
              
            }
        }
        private bool IsRowShow(OnlineStatus equipStatus)
        {
            if(DisplayStatus=="ALL")//要显示全部
            {
                return true;
            }
            else if (DisplayStatus=="ONLINE") //要显示在线的
            {
                return (equipStatus == OnlineStatus.在线);
            }
            else if (DisplayStatus == "OFFLINE") //要显示离线的
            {
                return (equipStatus == OnlineStatus.离线);
            }
            else
            {
                throw new Exception("参数不能识别");
            }
        }
        protected void ShowSortedIcon(GridView gvData, GridViewRow gvRow)
        {
            for (int index = 0; index <= gvData.Columns.Count - 1; index++)
            {
                if ((gvData.Columns[index].SortExpression == gvSortExpression) && (gvData.Columns[index].SortExpression != ""))
                {
                    Image img = new Image();
                    if ((string)ViewState["OrderDire"] == " ASC")
                    {
                        img.ImageUrl = "../../Images/asc.png";
                        img.ToolTip = "递增排列";
                    }
                    else
                    {
                        img.ImageUrl = "../../Images/desc.png";
                        img.ToolTip = "递减排列";
                    }
                    gvRow.Cells[index].Controls.Add(img);
                }
            }
        }
        private string gvSortExpression
        {
            get
            {
                if (ViewState["SortOrder"] != null && ViewState["SortOrder"].ToString() != string.Empty)
                { return ViewState["SortOrder"].ToString(); }
                else
                { return ""; }
            }
            set
            { ViewState["SortOrder"] = value; }
        }   
        protected void GridView_wince_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";

            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.GridView_wince.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.GridView_wince.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
                
                if (ViewState["OrderDire"].ToString() == " Desc")
                { ViewState["OrderDire"] = " ASC";
                ViewState["SortOrder"] = sortExpression;
               
                }
                else
                { ViewState["OrderDire"] = " Desc";
                ViewState["SortOrder"] = sortExpression;
                }    
            }

            // 重新设定GridView排序数据列及排序方向
            this.GridView_wince.Attributes["SortExpression"] = sortExpression;
            this.GridView_wince.Attributes["SortDirection"] = sortDirection;
            gvSortExpression = e.SortExpression;
            dbind();
        }

    }
}